<!-- pages/login/components/mobile.vue -->
<script setup>
	import { ref } from 'vue'
	import { verifyCodeApi, loginByMobileApi } from '@/services/user.js'
	import { useUserStore } from '@/stores/user.js'
	const showCountdown = ref(false)
	const userStore = useUserStore()
	const onTextButtonClick = async () => {
		try {
			await formRef.value.validateField(['mobile'])
			const { code, data, message } = await verifyCodeApi({
				mobile: formData.value.mobile,
				type: 'login',
			})
			if (code !== 10000) return uni.utils.toast(message)
			formData.value.code = data.code
			showCountdown.value = true
		} catch (error) {}
	}
	const onCountdownTimeup = () => {
		showCountdown.value = false
	}
	/**
	 * 提供了100个测试账号
	 * 手机号：13200000001 - 13200000100
	 * 密码：abc12345
	 */
	const formData = ref({
		mobile: '13212345678',
		code: '',
	})
	const onFormSubmit = async () => {
		if (!isAgree.value) return uni.utils.toast('请勾选协议！')
		try {
			const result = await formRef.value.validate()
			const { code, data, message } = await loginByMobileApi(result)
			if (code !== 10000) return uni.utils.toast(message)
			userStore.token = data.token
			userStore.userId = data.id
			// uni[userStore.openType]({
			// 	url: userStore.redirectURL,
			// })
			if (userStore.openType === 'switchTab') {
				uni.switchTab({
					url: userStore.redirectURL,
				})
			} else {
				uni.redirectTo({
					url: userStore.redirectURL,
				})
			}
		} catch (error) {}
	}
	const formRules = {
		mobile: {
			rules: [
				{ required: true, errorMessage: '请填写手机号码' },
				{ pattern: '^1\\d{10}$', errorMessage: '手机号码格式不正确' },
			],
		},
		code: {
			rules: [
				{ required: true, errorMessage: '请输入验证码' },
				{ pattern: '^\\d{6}$', errorMessage: '验证码格式不正确' },
			],
		},
	}
	const formRef = ref()
	const isAgree = ref(false)
	const onAgreeClick = () => {
		isAgree.value = !isAgree.value
	}
</script>

<template>
	<uni-forms
		class="login-form"
		ref="formRef"
		:model="formData"
		:rules="formRules"
	>
		<uni-forms-item name="mobile">
			<uni-easyinput
				:input-border="false"
				:clearable="false"
				v-model="formData.mobile"
				placeholder="请输入手机号"
				placeholder-style="color: #C3C3C5"
			/>
		</uni-forms-item>
		<uni-forms-item name="code">
			<uni-easyinput
				:input-border="false"
				:clearable="false"
				v-model="formData.code"
				placeholder="请输入验证码"
				placeholder-style="color: #C3C3C5"
			/>
			<view v-if="showCountdown" class="text-button">
				<uni-countdown
					:second="10"
					:show-day="false"
					:show-hour="false"
					:show-minute="false"
					color="#16C2A3"
					@timeup="onCountdownTimeup"
				></uni-countdown>
				秒后重新获取
			</view>
			<text v-else @click="onTextButtonClick" class="text-button"
				>获取验证码</text
			>
		</uni-forms-item>

		<view class="agreement">
			<radio :checked="isAgree" color="#16C2A3" @click="onAgreeClick" />
			我已同意
			<text class="link">用户协议</text>
			及
			<text class="link">隐私协议</text>
		</view>

		<button class="uni-button" @click="onFormSubmit">登 录</button>
	</uni-forms>
</template>

<script>
	export default {
		options: {
			styleIsolation: 'shared',
		},
	}
</script>

<style lang="scss">
	@import './styles.scss';
</style>
